//
//  MyMovieViewController.m
//  FNer
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 刘志强. All rights reserved.
//

#import "MyMovieViewController.h"
#import "DataHandle.h"
#import "MovieModel.h"
#import "MyMovieTablerViewCell.h"
#import "MovieDataViewController.h"
@interface MyMovieViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation MyMovieViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    NSLog(@"asd");
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [self.view addSubview:self.myTableView];
    
    
    self.allDataArray = [NSMutableArray array];
    self.allDataArray = [[DataHandle shareDataHandle] selectMovie];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.allDataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"cell";
    MyMovieTablerViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    MovieModel *model = [self.allDataArray objectAtIndex:indexPath.row];
    if (!cell) {
        cell = [[MyMovieTablerViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    cell.model = model;
    return cell;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MovieModel *model = [self.allDataArray objectAtIndex:indexPath.row];
    MovieDataViewController *movieDVC = [[MovieDataViewController alloc] init];
    movieDVC.model = model;
    [self.navigationController pushViewController:movieDVC animated:YES];
    
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //删除操作
        //先删数据
        MovieModel  *model = [self.allDataArray objectAtIndex:indexPath.row];
       
        [[DataHandle shareDataHandle] deleteMovie:model.title];
        [self.allDataArray removeObjectAtIndex:indexPath.row];
        //再删cell
        [self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:(UITableViewRowAnimationLeft)];
        
       // [self.myTableView  reloadData];
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
